package config

import (
	"errors"
	"os"
)

type DBConfig interface {
	GetDBDriver() string
	GetDBName() string
	GetDBUser() string
	GetDBPassword() string
	GetDBHost() string
	GetDBPort() string
	GetDBType() string
}

type dbConfig struct {
	driver   string `env:"DB_DRIVER" envDefault:"postgres"`
	username string `env:"DB_USERNAME" envDefault:"postgres"`
	password string `env:"DB_PASSWORD" envDefault:"postgres"`
	host     string `env:"DB_HOST" envDefault:"localhost"`
	dbPort   string `env:"DB_PORT" envDefault:"5432"`
	dbName   string `env:"DB_NAME" envDefault:"postgres"`
	dbType   string `env:"DB_TYPE" envDefault:"postgres"`
}

func NewDbConfig() (DBConfig, error) {
	driver := os.Getenv("DB_DRIVER")
	if len(driver) == 0 {
		return nil, errors.New("environment variable DB_DRIVER is required")
	}
	username := os.Getenv("DB_USERNAME")
	if len(username) == 0 {
		return nil, errors.New("environment variable DB_USERNAME is required")
	}
	password := os.Getenv("DB_PASSWORD")
	if len(password) == 0 {
		return nil, errors.New("environment variable DB_PASSWORD is required")
	}
	host := os.Getenv("DB_HOST")
	if len(host) == 0 {
		return nil, errors.New("environment variable DB_HOST is required")
	}
	dbPort := os.Getenv("DB_PORT")
	if len(dbPort) == 0 {
		return nil, errors.New("environment variable DB_PORT is required")
	}
	dbName := os.Getenv("DB_NAME")
	if len(dbName) == 0 {
		return nil, errors.New("environment variable DB_NAME is required")
	}
	dbType := os.Getenv("DB_TYPE")
	if len(dbType) == 0 {
		return nil, errors.New("environment variable DB_TYPE is required")
	}

	return &dbConfig{
		driver:   driver,
		username: username,
		password: password,
		host:     host,
		dbPort:   dbPort,
		dbName:   dbName,
		dbType:   dbType,
	}, nil
}

func (d *dbConfig) GetDBDriver() string {
	return d.driver
}

func (d *dbConfig) GetDBName() string {
	return d.dbName
}

func (d *dbConfig) GetDBUser() string {
	return d.username
}

func (d *dbConfig) GetDBPassword() string {
	return d.password
}

func (d *dbConfig) GetDBHost() string {
	return d.host
}

func (d *dbConfig) GetDBPort() string {
	return d.dbPort
}

func (d *dbConfig) GetDBType() string {
	return d.dbType
}
